package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.UserService;
import com.tarena.lbs.common.basic.converter.UserConverter;
import com.tarena.lbs.common.basic.converter.UserGroupConverter;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.param.UserParam;
import com.tarena.lbs.pojo.basic.param.UserTagsParam;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import com.tarena.lbs.pojo.basic.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserGroupConverter userGroupConverter;
    @Autowired
    private UserConverter userConverter;

    @PostMapping("/basic/user/info/register")
    public Result<Void> saveUser(@RequestBody UserParam userParam) throws BusinessException {
        log.debug("后台小程序创建用户入参 UserParam:{} ",userParam);
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(userParam,userBO);
        userService.saveUser(userBO);
        return Result.success();
    }


    @GetMapping("/basic/user/detail")
    public Result<UserVO> userDetail() throws BusinessException{
        log.debug("查询用户详细信息接口.....");
        UserBO userBO = userService.userDetail();
        UserVO userVO = null;
        if (!ObjectUtils.isEmpty(userBO)){
            userVO = new UserVO();
            BeanUtils.copyProperties(userBO,userVO);
        }
        return new Result<>(null);
    }

    @GetMapping("/basic/user/tag")
    public Result<Void> bindUserTags(UserTagsParam param)throws BusinessException{
        log.debug("绑定用户标签入参 UserTagsParam :{}",param);
        //要保证幂等性
        userService.bindUserTags(param);
        return Result.success();
    }

    @PostMapping("/admin/basic/userGroup/list")
    public Result<PageResult<UserGroupVO>> userGroupPage(@RequestBody UserGroupQuery userGroupQuery) throws BusinessException{
        log.debug("查询用户人群 入参UserGroupQuery:{}",userGroupQuery);
        BasePageList<UserGroupBO> boPage = userService.userGroupPage(userGroupQuery);
        return new Result<>(userGroupConverter.assemblePageVOS(boPage,userGroupQuery));
    }

    @PostMapping("/admin/basic/userGroup/save")
    public Result<Void> updateUserGroup(@RequestBody UserGroupParam param) throws BusinessException{
        log.debug("新增用户参数 UserGroupParam:{}",param);
        UserGroupBO userGroupBO = new UserGroupBO();
        BeanUtils.copyProperties(param,userGroupBO);
        userService.updateUserGroup(userGroupBO);
        return Result.success();
    }

}
